 
package com.xbongbong.saas.domain.dao;

import com.xbongbong.paas.domain.entity.StageLogEntity;
import com.xbongbong.saas.domain.entity.OpportunityStageLogEntity;
import com.xbongbong.saas.domain.entity.ext.StageLogEntityExt;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
* @author autoGen
* @time 2018-06-06 19:45:16
*/
@Repository
@Mapper
public interface OpportunityStageLogDao {

	Integer insert(OpportunityStageLogEntity opportunityStageLogEntity);
	Integer update(OpportunityStageLogEntity opportunityStageLogEntity);

	/**
	 * 批量插入
	 * @author 吴峰
	 * @date 2019/3/20 13:43
	 * @param opportunityStageLogEntityList
	 */
	void insertBatch(List<OpportunityStageLogEntity> opportunityStageLogEntityList);
	Integer deleteByStageProcessIdAndDataIdIn(@Param("list") List<Long> dataIdIn, @Param("corpid") String corpid, @Param("formId") Long formId, @Param("stageProcessId") Long stageProcessId, @Param("stageId") Long stageId);
	Integer deleteByKey(@Param("key") Long key, @Param("corpid") String corpid);
	OpportunityStageLogEntity getByKey(@Param("key") Long key, @Param("corpid") String corpid);

	List<OpportunityStageLogEntity> findEntitys(@Param("param") Map<String, Object> param);
	Integer getEntitysCount(@Param("param") Map<String, Object> param);

	/**
	 * 获取某个阶段流程下是否存在关联的机会数据
	 * 不关注机会数据是否已经在主表被删除
	 *
	 * @param corpid 公司id
	 * @param formId 表单id
	 * @param stageProcessId 阶段流程id
	 * @return OpportunityStageLogEntity
	 */
	OpportunityStageLogEntity existOpportunityByStageProcessId(@Param("corpid") String corpid, @Param("formId") Long formId, @Param("stageProcessId") Long stageProcessId);

	List<StageLogEntityExt> getAverageStayTime(@Param("param")Map<String ,Object> param);

	/**
	 * 按照最近一次跳转的跳入时间，在固定时间范围内的销售机会id
	 * @param param
	 * @return
	 */
	List<Long> getLastStageLogIdsByIntoTime(@Param("param")Map<String ,Object> param);

	/**
	 * 批量更新
	 * @author 吴峰
	 * @date 2019/3/20 13:43
	 * @param opportunityStageLogList
	 * @param corpid 公司id
	 * @return java.util.List<com.xbongbong.saas.domain.entity.OpportunityStageLogEntity>
	 */
	void updateBatch(@Param("list") List<OpportunityStageLogEntity> opportunityStageLogList,@Param("corpid") String corpid);

	void updateBatch4Script(@Param("list") List<OpportunityStageLogEntity> opportunityStageLogList);

	List<OpportunityStageLogEntity> findStageLogWithoutStage();

	Integer deleteBatch(@Param("list") List<StageLogEntity> stageLogEntityList, @Param("corpid") String corpid);

	/**
	 * 根据dataId 查找当前数据所处阶段
	 * @param corpid
	 * @param formId
	 * @param dataId
	 * @return
	 */
	OpportunityStageLogEntity getIsNowStage(@Param("corpid") String corpid, @Param("formId") Long formId, @Param("dataId") Long dataId);

	Long getAvgStayTime(@Param("corpid") String corpid, @Param("stageProcessId") Long stageProcessId, @Param("stageId") Long stageId);

	List<Long> findUsedStageProcessId(@Param("corpid") String corpid, @Param("formId") Long formId, @Param("stageProcessIdIn") List<Long> stageProcessIdIn);
}